/*
 * Created on 2005-11-14
 *
 */
package com.yl.cap.analyst.report.chart.maker;

import java.awt.Color;
import java.io.FileOutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.RectangleEdge;

import com.yl.cap.analyst.report.chart.data.ChartData;
import com.yl.cap.analyst.report.chart.data.DualAxisChartData;

public class HorizontalDualAxisMaker extends ChartMaker
{

    public void makeChart(String fileName, ChartData data) throws Exception
    {
        DualAxisChartData chartData = (DualAxisChartData)data;
        //create dataset
        CategoryDataset barDataset = DatasetUtilities.createCategoryDataset(
                chartData.getBarData().getRowKeys(), chartData.getBarData().getColumnKeys(), chartData.getBarData().getData());
        CategoryDataset lineDataSet = DatasetUtilities.createCategoryDataset(
                chartData.getLineData().getRowKeys(), chartData.getLineData().getColumnKeys(), chartData.getLineData().getData());
        //=============================
        JFreeChart jfreechart = ChartFactory.createBarChart(data.getCaption(), chartData.getXCaption(), chartData.getYCaption(), barDataset, PlotOrientation.HORIZONTAL, true, true, false);
        jfreechart.setBackgroundPaint(new Color(204, 255, 204));
        LegendTitle legendtitle = (LegendTitle)jfreechart.getSubtitle(0);
        legendtitle.setPosition(RectangleEdge.LEFT);
        CategoryPlot categoryplot = jfreechart.getCategoryPlot();
        categoryplot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
        categoryplot.getDomainAxis().setMaximumCategoryLabelWidthRatio(10F);
        NumberAxis numberaxis = new NumberAxis(chartData.getZCaption());
        categoryplot.setRangeAxis(1, numberaxis);
        categoryplot.setDataset(1, lineDataSet);
        categoryplot.mapDatasetToRangeAxis(1, 1);
        categoryplot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
        LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
        categoryplot.setRenderer(1, lineandshaperenderer);
        categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
        //===========================
        FileOutputStream fos_jpg = null;
        try
        {
            fos_jpg = new FileOutputStream(fileName);
            ChartUtilities.writeChartAsJPEG(fos_jpg, 100, jfreechart, data
                    .getWidth(), data.getHeight(), null);
        }
        catch (Exception e)
        {
            fos_jpg.close();
            throw e;
        }
        fos_jpg.close();
    }
}
